#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266WebServer.h>
#define LAMP_PIN_OUTPUT D5

ESP8266WiFiMulti wifiMulti;     // 建立ESP8266WiFiMulti对象,对象名称是'wifiMulti'
ESP8266WebServer esp8266_server(80);// 建立网络服务器对象，该对象用于响应HTTP请求。监听端口（80）
 
void setup(){
  Serial.begin(9600);          // 启动串口通讯
  delay(10);
  pinMode(LAMP_PIN_OUTPUT,OUTPUT);     //D5设置为输出模式
  
  wifiMulti.addAP("lamp", "11111111"); // WiFi和密码
                              
  while (wifiMulti.run() != WL_CONNECTED) {  //模块连接wifi
    delay(1000);                             
  }                                          
  Serial.print("Connected to ");            // NodeMCU将通过串口监视器输出。
  Serial.println(WiFi.SSID());              // 连接的WiFI名称以及
  Serial.print("IP address:\t");            
  Serial.println(WiFi.localIP());           // NodeMCU的IP地址
  
  esp8266_server.begin();                  
  esp8266_server.on("/", handleRoot);
  esp8266_server.on("/lamp", handleLamp);     
  esp8266_server.onNotFound(handleNotFound);        
}
 
void loop(){
  esp8266_server.handleClient();     // 处理http服务器访问
}                                                                   
   
void handleRoot() {   //处理网站目录“/”的访问请求 
  esp8266_server.send(200, "text/html", sendHTML());  
}

void handleLamp() {   //处理网站目录“/lamp”的访问请求
  String status = esp8266_server.arg("status");
  if(status == "1"){
      digitalWrite(LAMP_PIN_OUTPUT, HIGH);
  }else{
      digitalWrite(LAMP_PIN_OUTPUT, LOW);
  }
  
}

String sendHTML(){
  String htmlCode = "<!DOCTYPE html>\n";
  htmlCode += "<html lang='zn'>\n";
  htmlCode += "<head>\n";
  htmlCode += "<meta charset='UTF-8'>\n";
  htmlCode += "<title>Welcome</title>\n";
  htmlCode += "</head>\n";
  htmlCode += "<body style='display: flex; justify-content: center; align-items: center;'>\n";
  htmlCode += "<form action='/lamp' method='post' id='form' target='posthere'>\n";
  htmlCode += "<input type='hidden' id='s' name='status' value='0'>\n";
  htmlCode += "</form>\n";
  htmlCode += "<div style='width: 50%; height: 50%;' id='btn'>\n";
  htmlCode += "</div>\n";
  htmlCode += "<iframe name='posthere' frameborder='0' height='0' width='0'></iframe>\n";
  htmlCode += "</body>\n";
  htmlCode += "<script>\n";
  htmlCode += "var form = document.getElementById('form');\n";
  htmlCode += "var s = document.getElementById('s');\n";
  htmlCode += "var btn = document.getElementById('btn');\n";
  htmlCode += "btn.onclick = clickEvent\n";
  htmlCode += "window.onload=function(){\n";
  htmlCode += "value = s.getAttribute('value');\n";
  htmlCode += "if(value == 0){\n";
  htmlCode += "closeStyle()\n";
  htmlCode += "}else{\n";
  htmlCode += "onStyle()\n";
  htmlCode += "}\n";
  htmlCode += "}\n";
  htmlCode += "function clickEvent() {\n";
  htmlCode += "value = s.getAttribute('value')\n";
  htmlCode += "if (value == 0) {\n";
  htmlCode += "value = 1\n";
  htmlCode += "s.setAttribute('value', value)\n";
  htmlCode += "onStyle()\n";
  htmlCode += "}\n";
  htmlCode += "else {\n";
  htmlCode += "value = 0\n";
  htmlCode += "s.setAttribute('value', value)\n";
  htmlCode += "closeStyle()\n";
  htmlCode += "}\n";
  htmlCode += "form.submit()\n";
  htmlCode += "}\n";
  htmlCode += "function onStyle(){\n";
  htmlCode += "btn.innerHTML = \"<svg t='1650090107561' class='icon' viewBox='0 0 1024 1024' version='1.1' xmlns='http://www.w3.org/2000/svg' p-id='5613' ><path d='M512 983.04A434.87744 434.87744 0 0 1 317.40928 159.09376a36.24448 36.24448 0 1 1 32.47104 64.81408 362.65472 362.65472 0 1 0 306.688-8.192 36.24448 36.24448 0 1 1 28.94336-66.4576A434.88256 434.88256 0 0 1 512 983.04z m1.36192-490.30656a36.25472 36.25472 0 0 1-36.26496-36.23936V82.31936a36.27008 36.27008 0 0 1 72.53504 0v374.17472a36.25472 36.25472 0 0 1-36.27008 36.23936z' fill='#0846ac' p-id='5614'></path></svg>\"\n";
  htmlCode += "}\n";
  htmlCode += "function closeStyle(){\n";
  htmlCode += "btn.innerHTML = \"<svg t='1650090107561' class='icon' viewBox='0 0 1024 1024' version='1.1' xmlns='http://www.w3.org/2000/svg' p-id='5613'><path d='M512 983.04A434.87744 434.87744 0 0 1 317.40928 159.09376a36.24448 36.24448 0 1 1 32.47104 64.81408 362.65472 362.65472 0 1 0 306.688-8.192 36.24448 36.24448 0 1 1 28.94336-66.4576A434.88256 434.88256 0 0 1 512 983.04z m1.36192-490.30656a36.25472 36.25472 0 0 1-36.26496-36.23936V82.31936a36.27008 36.27008 0 0 1 72.53504 0v374.17472a36.25472 36.25472 0 0 1-36.27008 36.23936z' fill='#0f0a32' p-id='5614'></path></svg>\"\n";
  htmlCode += "}\n";
  htmlCode += "</script>\n";
  htmlCode += "</html>\n";
  return htmlCode;
}
 
// 设置处理404情况的函数'handleNotFound'
void handleNotFound(){                                        // 当浏览器请求的网络资源无法在服务器找到时，
  esp8266_server.send(404, "text/plain", "Not found");   // NodeMCU将调用此函数。
}
